import * as Cesium from 'cesium/Cesium'
import DrawingMode from "./DrawingMode";
import MeasureUnits from "./MeasureUnits";
import MeasurementSettings from "./MeasurementSettings";
import HorizontalPolylineDrawing from "./HorizontalPolylineDrawing";


var cart3Scratch$3 = new  Cesium.Cartesian3
    , cart2Scratch1$2 = new  Cesium.Cartesian2
    , cart2Scratch2$2 = new  Cesium.Cartesian2
    , cart2Scratch3 = new  Cesium.Cartesian2
    , v1Scratch$3 = new  Cesium.Cartesian3;

function HorizontalMeasurementDrawing(e) {
    e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT),
        HorizontalPolylineDrawing.call(this, e);
    var t = e.labels;
    this._labelCollection = t,
        this._label = t.add(MeasurementSettings.getLabelOptions({
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(10,-10)
        })),
        this._segmentLabels = [],
        this._selectedUnits = e.units,
        this._selectedLocale = e.locale,
        this._previousDistance = 0,
        this._distance = 0;
    var i = this;
    this._removeEvent = this._scene.preRender.addEventListener(function() {
        i.updateLabels()
    })
}

HorizontalMeasurementDrawing.prototype = Object.create(HorizontalPolylineDrawing.prototype),
    HorizontalMeasurementDrawing.prototype.constructor = HorizontalMeasurementDrawing,
    Cesium.defineProperties(HorizontalMeasurementDrawing.prototype, {
        distance: {
            get: function() {
                return this._distance
            }
        }
    }),
    HorizontalMeasurementDrawing.prototype.updateLabels = function() {
        var e = this._positions;
        if (!(e.length < 2)) {
            var t = this._scene
                , i = e[0]
                , r = Cesium.SceneTransforms.wgs84ToWindowCoordinates(t, i, cart2Scratch1$2)
                , n = Cesium.defined(r) ? Cesium.Cartesian2.clone(r, cart2Scratch3) : Cesium.Cartesian2.fromElements(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, cart2Scratch3)
                , a = n.y
                , o = this._segmentLabels;
            o[0].show = 2 < this._polyline.positions.length;
            for (var s = 1; s < e.length; s++) {
                var l = Cesium.SceneTransforms.wgs84ToWindowCoordinates(t, e[s], cart2Scratch2$2);
                if (Cesium.defined(l)) {
                    var c = (n.y - l.y) / (l.x - n.x)
                        , u = o[s - 1];
                    u.horizontalOrigin = 0 < c ? Cesium.HorizontalOrigin.LEFT : Cesium.HorizontalOrigin.RIGHT,
                    l.y < a && (a = l.y,
                        i = e[s]),
                        n = Cesium.Cartesian2.clone(l, n)
                }
            }
            this._mode === DrawingMode.AfterDraw && (this._label.position = i)
        }
    }
    ,
    HorizontalMeasurementDrawing.prototype.addPoint = function(e) {
        var t = this._positions;
        if (0 < t.length) {
            this._previousDistance = this._distance;
            var i = this._labelCollection.add(MeasurementSettings.getLabelOptions({
                scale: .8,
                horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                verticalOrigin: Cesium.VerticalOrigin.TOP,
                pixelOffset: new Cesium.Cartesian2(5,5)
            }))
                , r = t[t.length - 1];
            i.position = Cesium.Cartesian3.midpoint(r, e, new Cesium.Cartesian3),
                i.text = MeasureUnits.distanceToString(Cesium.Cartesian3.distance(r, e),
                    this._selectedUnits.distanceUnits, this._selectedLocale),
                i.show = !0,
                this._segmentLabels.push(i)
        }
        HorizontalPolylineDrawing.prototype.addPoint.call(this, e)
    }
    ,
    HorizontalMeasurementDrawing.prototype.handleClick = function(e) {
        this._mode === DrawingMode.AfterDraw && this.reset(),
        Cesium.defined(HorizontalPolylineDrawing.prototype.handleClick.call(this, e)) && (this._label.show = !0,
            this._polyline.show = !0)
    }
    ,
    HorizontalMeasurementDrawing.prototype.handleMouseMove = function(e, t) {
        var i = HorizontalPolylineDrawing.prototype.handleMouseMove.call(this, e, t);
        if (Cesium.defined(i)) {
            var r = this._positions
                , n = r[r.length - 1]
                , a = Cesium.Cartesian3.subtract(i, n, v1Scratch$3)
                , o = this._previousDistance + Cesium.Cartesian3.magnitude(a)
                , s = this._label;
            s.position = Cesium.Cartesian3.midpoint(n, i, cart3Scratch$3),
                s.text = MeasureUnits.distanceToString(o, this._selectedUnits.distanceUnits, this._selectedLocale),
                s.show = !0,
                this._distance = o
        }
    }
    ,
    HorizontalMeasurementDrawing.prototype.reset = function() {
        var e, t = this._primitives, i = this._dashedLines;
        for (e = 0; e < i.length; e++)
            t.remove(i[e]);
        this._dashedLines = [],
            this._polyline.positions = [],
            this._polyline.show = !1,
            this._label.show = !1,
            this._label.text = "",
            this._previousDistance = 0,
            this._distance = 0,
            this._positions = [];
        var r = this._points
            , n = this._pointCollection;
        for (e = 0; e < r.length; e++)
            n.remove(r[e]);
        r.length = 0;
        var a = this._labelCollection
            , o = this._segmentLabels;
        for (e = 0; e < o.length; e++)
            a.remove(o[e]);
        o.length = 0,
            this._moveDashLine.show = !1,
            this._mode = DrawingMode.BeforeDraw,
            this._lastClickPosition.x = Number.POSITIVE_INFINITY,
            this._lastClickPosition.y = Number.POSITIVE_INFINITY
    }
    ,
    HorizontalMeasurementDrawing.prototype.destroy = function() {
        var e;
        this._removeEvent();
        var t = this._labelCollection
            , i = this._segmentLabels;
        for (e = 0; e < i.length; e++)
            t.remove(i[e]);
        var r = this._primitives
            , n = this._dashedLines;
        for (e = 0; e < n.length; e++)
            r.remove(n[e]);
        this._labelCollection.remove(this._label),
            HorizontalPolylineDrawing.prototype.destroy.call(this)
    }
    export default HorizontalMeasurementDrawing;